<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!doctype html>
<html>
<head>
	<title>添加配件</title>
	<%@ include file="../common/base.jsp" %>
</head>

<body id="body">
<!--head-->

<!--head end-->
<!--container-->
<div id="container">
	<!--main-->
	<div class="main">
		<!--content-->
		<div class="content">
			<div id="tableResult">
				<!--权限列表-->
				<div class="listBox">
					<ul id="rightTree">
					</ul>
				</div>
				<!--权限列表-->
			</div>

		</div>
		<div class="button_wrap button_wrap2">
			<a href="javascript:;" class="bg_blue" onClick="closeDialog();">确定</a>
		</div>
	</div>
	<!--main-->
</div>
<!--container-->
<script src="${pageContext.request.contextPath}/static/js/basic.js"></script>
<script type="text/javascript">

    var TOP_MENUID = "0";

    function closeDialog(){
        var id="";
        var partsName="";
        $("input[name='partsId']").each(function(){
            var ch = $(this).attr("checked");
            var chId = $(this).val();
            if(chId.indexOf(id) >= 0){
                id="";
                partsName="";
			}

            if(ch == "checked") {
                id = chId;
                partsName = $(this).attr("partsName");
            }
		});

        if(id ==""){
            alert("请选择最子节点的配件进行添加！");
            return ;
		}

        self.close();
        window.returnValue =  id+","+partsName;
	}


    //初始化加载渠道列表、权限树相关数据
    function init(parentId){
        $("#tableResult").showLoading();
        $.ajax({
            type: "POST",
            url: "${pageContext.request.contextPath}/parts/queryPartsTree",
            dataType:"json",
            data:{
                parentId:parentId
            },
            success: function(data){
                var rightData = data.rightData;

                //绑定权限树数据
                for (var j = 0; j < rightData.length; j++) {
                    createPartsTree(rightData[j]);
                }
                $("#tableResult").hideLoading();
            }
        });
    }



    //权限树展开所有
    function spreadAll(){

        if ($("#spreadAll").text()=="全部展开") {
            $("#rightTree ul").show();
            $("#spreadAll").text("全部收缩");
        } else {
            $("#rightTree ul").hide();
            $("#spreadAll").text("全部展开");
        }
    }
    //权限树单个展开收缩
    function spread(clsName){
        $("."+clsName).each(function(i,e){
            if ($(e).is(":hidden")) {
                $(e).show();
            } else {
                $(e).hide();
            }
        });
    }


    //修改时回写权限树中checkbox后父级节点的处理
    function initRightCb(){
        var flag = false;
        $("#rightTree input").each(function(i,e){
            if (flag=$(e).attr("checked")) {
                var menuId = $(e).attr("value");
                var parentMenuId = $("#"+menuId).attr("value");
                dealParent(parentMenuId, flag);
            }
        });
    }

    //权限树checkbox全选
    function selectCheckBox(){
        if ($("#selectAll").attr("checked")) {
            $("#rightTree input").attr("checked",true);
        } else {
            $("#rightTree input").attr("checked",false);
        }
    }

    //权限树单个checkbox处理方法
    function selectCb(menuId,parentMenuId){
        var flag = false;
        //子节点处理
        $("."+menuId+" input").each(function(i,e){//查询子元素下所有checkbox
            if ($("#"+menuId+" input:eq(0)").attr("checked")) {//当前checkbox
                $(e).attr("checked",true);
            } else {
                $(e).attr("checked",false);
            }
        });

        if ($("#"+menuId+" input:eq(0)").attr("checked")) {
            flag = true;
        }
        dealParent(parentMenuId,flag);
    }

    //权限树checkbox父级节点相关处理
    function dealParent(parentMenuId,flag){
        var run = true;
        var mark = true;
        if ($("#"+parentMenuId+" input:eq(0)").length>0) {//检查是否有父节点
            if (!flag) {
                $("."+parentMenuId+" input").each(function(i,e){//检查同级元素是否有选中的
                    if ($(e).attr("checked")) {
                        run = false;
                        return run;
                    }
                });
            }else {
                $("#rightTree input").each(function(i,e){//查询是否全部选中
                    if (!$(e).attr("checked") && $(e).attr("id")!="selectAll") {
                        mark = false;
                        return mark;
                    }
                });
            }
            if (run) {
                $("#"+parentMenuId+" input:eq(0)").attr("checked",flag);
                if (!mark) {
                    $("#selectAll").attr("checked",mark);//取消全选按钮√
                }
                dealParent($("#"+parentMenuId).attr("value"),flag);
            }else {
                $("#selectAll").attr("checked",run);//取消全选按钮√
            }
        }
    }

    //创建权限树
    function createPartsTree(obj){
        var li = "<li id="+obj.id+" value="+obj.parentId+"><input name='partsId' type='radio' value='"+obj.id+"' partsName='"+obj.partsName+"'>" +
			"<span style='cursor:pointer;' onclick='spread("+obj.id+");'>"+obj.partsName+"</span></li>";
        if (obj.parentId==TOP_MENUID) {//一级菜单
            $("#rightTree").append(li);
            $("#"+obj.id+" input").attr("class",obj.parentId);
        }else {
            $("#"+obj.parentId).append("<ul class="+obj.parentId+" style='display:none;'>"+li+"</ul>");//找父节点追加元素
        }

        if (obj.SUB_DATA!=undefined) {
            $("#"+obj.id+" span").before("<i class='triangle_l'/>");
            for (var i = 0; i < obj.SUB_DATA.length; i++){
                createPartsTree(obj.SUB_DATA[i]);
            }
        }

    }

    $("#rightTree li").attr("id",TOP_MENUID);
    init();

</script>
</body>
</html>